package pdm.traj.service.impl;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import pdm.traj.entity.Station;
import pdm.traj.service.GeoService;
import pdm.traj.util.TrackPointUtil;

public class GeoServiceImpl implements GeoService {
	private String srcPath;
	private Map<String, Station> map;
	private List<String> allSid;
	
	public void init() {
		map = new HashMap<String, Station>();
		allSid = new ArrayList<String>();
		if (srcPath == null || "".equals(srcPath)) {
			return;
		}
        Resource resource = new ClassPathResource(srcPath);
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
			while (br.ready()) {
				String l = br.readLine();
				StringTokenizer st = new StringTokenizer(l, "\t", false);
				st.nextToken();
				String lacid = st.nextToken();
				String cellid = st.nextToken();
				String lngStr = st.nextToken();
				String latStr = st.nextToken();
				map.put(lacid + "_" + cellid, new Station(lacid, cellid, 
						Double.parseDouble(lngStr), Double.parseDouble(latStr)));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (NoSuchElementException msee) {
			msee.printStackTrace();
		}
		makeAllSidList();
	}
	
	private void makeAllSidList() {
		for (String s : map.keySet()) {
			allSid.add(s);
		}
	}
	
	public void printMap() {
		int i = 0;
		for (Entry v : map.entrySet()) {
			System.out.println(++i + "\t" + "key: " + v.getKey() + "\t" + "val: " + v.getValue());
//			System.out.println("key: " + v + "\t" + "val: " + map.get(v));
		}
	}
	
	@Override
	public List<String> getAllSids() {
		List<String> result = new ArrayList<String>();
		for (String s : allSid) {
			result.add(s);
		}
		return result;
	}

	@Override
	public boolean validSid(String sid) {
//		return map.containsKey(sid);
		return true;
	}
	
	public String getSrcPath() {
		return srcPath;
	}
	public void setSrcPath(String srcPath) {
		this.srcPath = srcPath;
	}

	@Override
	public List<String> getSidsByRegion(double x1, double y1, double x2,
			double y2) {
		List<String> sids = new ArrayList<String>();
		for (Station s : map.values()) {
			double lng = s.getLng();
			double lat = s.getLat();
			if (lng >= x1 && lng <= x2  && lat >= y1 && lat <= y2) {
				sids.add(s.getSid());
			}
		}
		return sids;
	}

	@Override
	public List<String> getAllSidsByLacid(String lacid) {
		if (lacid == null) {
			return null;
		} else {
			List<String> result = new ArrayList<String>();
			for (String sid : allSid) {
				if (lacid.equals(TrackPointUtil.getLacidFromSid(sid))) {
					result.add(sid);
				}
			}
			return result;
		}
	}

    @Override
    public double getLongitudeBySid(String sid) {
        Station station = map.get(sid);
        if (station != null) {
            return station.getLng();
        } else {
            return GeoService.LNG_NOT_EXIST;
        }
    }

    @Override
    public double getLatitudeBySid(String sid) {
        Station station = map.get(sid);
        if (station != null) {
            return station.getLat();
        } else {
            return GeoService.LAT_NOT_EXIST;
        }
    }


}
